package cc.linker.steplink.crm.merchanth5.controller.acs;

import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.acs.H5RechargeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/h5/recharge")
public class H5RechargeController {

    @Autowired
    private H5RechargeService h5RechargeService;

    @Autowired
    private RequestFilter requestFilter;

    /**
     * @Description: 获取充值信息
     * @Param: rechargeSn 充值单号
     * @Return: java.util.Map
     * @Author: ch
     * @Date: 2020/7/16 9:34
     * @Version: 1.12.6
     **/
    @GetMapping("/getRechargeInfo")
    public Map getRechargeInfo(String rechargeSn){
        Map rechargeInfo = h5RechargeService.getRechargeInfo(rechargeSn);
        return rechargeInfo;
    }


    /**
     * 创建订单
     *
     * @param : rechargeSn 充值单号
     */
    @PostMapping("/rechargeByRechargeSn")
    public Map createOrder(@RequestBody Map<String, Object> param, HttpServletRequest request)  {
        // 创建订单，生成返润体系，然后调中金得到支付签名，返回给前端
        WxUser wxUser = requestFilter.getWxUser(request);
        param.put("customerId", wxUser.getCustomerId());
        param.put("orgId", wxUser.getOrgId());
        param.put("openid", wxUser.getPayOpenId());
        param.put("payMode", wxUser.getPayMode());
        //WxUser wxUser =  requestFilter.getWxUser(request);
        return h5RechargeService.rechargeByRechargeSn(param);
    }

    /**
     * @Description: 充值回调
     * @Param: [params]
     * @Return: void
     * @Author: ch
     * @Date: 2020/7/16 9:18
     * @Version:  1.12.6
     **/
    @PostMapping("/ordernotify")
    public void orderNotify(@RequestBody Map<String, Object> params) throws Exception {
        params.put("tradeSn", params.get("transactionId"));
        params.put("rechargeSn", params.get("outTradeNo"));
        //修改充值记录状态
        h5RechargeService.callback(params);
    }
}
